package com.lanswon.qzsmk.controller.inventoryManageController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.InventoryException;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.CardControl;
import com.lanswon.qzsmk.model.CardTask;
import com.lanswon.qzsmk.service.systemManageService.InventoryManagementService;

import ch.qos.logback.classic.Logger;

@Controller
@RequestMapping(value = "/inventoryDistributionMagOfCenter")
public class InventoryDistributionMagOfCenterController {
	
	private final static Logger logger = (Logger) LoggerFactory.getLogger(InventoryDistributionMagOfCenterController.class);
	
	@Autowired
	InventoryManagementService ims;
	
	/**
	 * 子模块：采购卡配送 功能：跳转卡片配送页面
	 * 
	 * @return
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:index")
	@RequestMapping(name = "中心库配送出库,inventoryDistributionMagOfCenter:index", value = "/cardDistribution", method = RequestMethod.GET)
	@SystemControllerLog(description = "跳转卡片配送页面")
	public String cardDistribution(HttpServletRequest request) {
		return "CardDistribution";
	}

	/**
	 * 子模块：采购卡配送 
	 * 功能：获取配送卡的详细信息
	 * @return
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:getDistributionDetail")
	@RequestMapping(name = "获取配送信息,inventoryDistributionMagOfCenter:getDistributionDetail",value = "/getDistributionDetail", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "获取配送卡的详细信息")
	public Map<String, Object> getTaskDetail(HttpServletRequest request, Integer pageNum,Integer numPerPage) {
		if (pageNum == null && numPerPage == null) {
			pageNum = 1;
			numPerPage = 10;
		}
		// 接收前画面穿过来数据
		String cardPCode = request.getParameter("cardpCode");
		String branchNo = request.getParameter("applyBranchNo");
		String taskNo = request.getParameter("taskNo");
		String taskName = request.getParameter("taskName");
		logger.debug("cardPCode==>" + cardPCode);
		logger.debug("branchNo==>" + branchNo);
		logger.debug("taskNo==>" + taskNo);
		logger.debug("taskName==>" + taskName);
		// 装有画面信息的map
		Map<String, String> map = new HashMap<String, String>();
		map.put("cardPCode", cardPCode);
		map.put("branchNo", branchNo);
		map.put("taskNo", taskNo);
		map.put("taskName", taskName);
		DwzPage<CardTask> taskDPage = ims.queryTaskInfo(map, pageNum, numPerPage);
		Map<String, Object> rMap = new HashMap<>();
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		rMap.put("activeUser", activeUser);
		rMap.put("taskDPage", taskDPage);
		return rMap;
	}

	/**
	 * 模块：采购卡配送
	 * 功能：查询配送的详细信息
	 * @return
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:getDistributionDetail")
	@RequestMapping(name = "查询配送信息,inventoryDistributionMagOfCenter:getDistributionDetail",value = "/getDistributionDetail", method = RequestMethod.GET)
	@ResponseBody
	@SystemControllerLog(description = "查询配送的详细信息")
	public Map<String, Object> getDistributionDetail(HttpServletRequest request) {
		// 读取页面传过来的checked值
		String checkedInfo = (String) request.getSession().getAttribute("checkedInfo");
		logger.debug("checkedInfo==>" + checkedInfo);
		request.getSession().removeAttribute("checkedInfo");
		// 将页面选中的信息，传入service方法中，并处理，读出对应的卡片信息
		// 存取登记画面上，table的信息和input框的信息的map
		Map<String, Object> rmap = new HashMap<String, Object>();
		rmap = ims.getEditDistributionMap(checkedInfo);
		// 取得taskNolist的值
		List<String> taskLists = (List<String>) rmap.get("taskNoList");
		// 取得cardPCodeList的值
		List<String> cardPCodeLists = (List<String>) rmap.get("cardPCodeList");
		// 原有的tablelist
		// //存取Table信息的LIst值
		List<CardControl> tableList = new ArrayList<CardControl>();
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		rmap.put("activeUser", activeUser);

		return rmap;
	}
	/**
	 * 子模块：采购卡配送 
	 * 功能：处理跳转卡卡片配送登记页面的参数
	 * @param lossCard
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:toCardDisRegister")
	@RequestMapping(name = "查询配送信息,inventoryDistributionMagOfCenter:toCardDisRegister",value = "/toCardDisRegister", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "处理跳转卡片配送登记页面的参数")
	public Map<String, Object> toLoosCardrRegister(String[] distributionTasked, HttpServletRequest request) {
		//
		Map<String, Object> map = new HashMap<>();
		// 选中的信息拼成的字符串
		String tasksChecked = null;
		// 解析被选中的信息
		for (int j = 0; j < distributionTasked.length; j++) {
			logger.debug("传入的选中信息是==>" + distributionTasked[j]);
			if (j == 0) {
				tasksChecked = distributionTasked[0];
			} else {
				tasksChecked = tasksChecked + "$" + distributionTasked[j];
			}
		}
		request.getSession().removeAttribute("checkedInfo");
		request.getSession().setAttribute("checkedInfo", tasksChecked);
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "inventoryDistributionMagOfCenter/cardDisReg");// post请求
		return map;
	}

	/**
	 * 子模块：采购卡配送 
	 * 功能：跳转配送登记页面
	 * @return
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:cardDisReg")
	@RequestMapping(name = "跳转登记页面,inventoryDistributionMagOfCenter:cardDisReg",value = "/cardDisReg", method = RequestMethod.POST)
	@SystemControllerLog(description = "跳转配送登记页面")
	public String cardDisReg(HttpServletRequest request) {
		return "CardDistributionRegister";
	}

	/**
	 * 子模块：采购卡配送 
	 * 功能：配送登记
	 * @param request
	 * @return
	 * @throws InventoryException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("inventoryDistributionMagOfCenter:distributeRegister")
	@RequestMapping(name = "配送登记,inventoryDistributionMagOfCenter:distributeRegister",value = "/distributeRegister", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "配送登记")
	public Map<String, Object> distributeRegister(HttpServletRequest request) throws InventoryException {
		// 返回map对象
		Map<String, Object> map = new HashMap<String, Object>();
		String serializeForm = request.getParameter("serializeForm");
		logger.debug("serializeForm==>" + serializeForm.toString());
		// 配送登记后，返回的打印清单对象
		Map<String, Object> rMap = new HashMap<String, Object>();
		// 更新表的信息
		rMap = ims.distributeRegistMethod(serializeForm);
		Map<String,Object> pramMap = new HashMap<String,Object>();
		pramMap.put("applyType", rMap.get("credential_type"));
		map.put("ipAddress", rMap.get("ipAddress"));
		map.put("storageNo", rMap.get("storageNo"));
		map.put("max", "true");
		map.put("title", "配送");
		map.put("rel", "distributePrint");
		map.put("message", "配送出库成功");
		map.put("statusCode", "200");
		map.put("param", pramMap);
		
		map.put("jumpToTabid", "cardDistribution");
		map.put("jumpToUrl", "inventoryDistributionMagOfCenter/cardDistribution");
		map.put("jumpToTile", "中心库配送出库");
		map.put("url", "printCredentials/toStoragePdfView");// post请求
		return map;
	}
	
}
